using System;
using System.Collections.Generic;
using NUnit.Framework;
using RMH.DataAccess;
using RMH.Entities;
using RMH.UnitTests;
using RMH.Web;

namespace RMH.IntegrationTests.Repository
{
    [TestFixture]
    public class PatientRepository_when_presisting : RepositoryBaseSpecs
    {
        private CountyRepository countyRepository;
        private County county;
        private Patient patient;
        private SupportPerson supportPerson;
        private Stay stay;
        private string body = "woot.";
        DateTime date = new DateTime(1989, 2, 2);

        public override void establish_context()
        {
            countyRepository = Container.Resolve<CountyRepository>();
            supportPerson = Container.Resolve<SupportPersonRepository>().GetFirst();
            county = countyRepository.GetByID(1);
            patient = new Patient { FirstName = "John", LastName = "Doe", County = county };
            patient.Address = new Address
                                  {
                                      City = "Hunlock Creek",
                                      State = "PA",
                                      Street1 = "Box 448",
                                      Street2 = "Sorbertown Hill",
                                      PostalCode = "48103"
                                  };
            stay = new Stay {CheckinDate = DateTime.Today, Patient = patient};
            patient.Stays.Add(stay);
            
        }

        public override void because()
        {
            var patientRepository = Container.Resolve<PatientRepository>();
            patientRepository.Save(patient);
            patientRepository.Flush();
            
            patient.Notes.Add(new Note { Body = body, Date = date, StaffId = "Foo", Patient = patient });
            patient.Diagnoses.Add(Container.Resolve<DiagnosisRepository>().GetFirst());
            var relationship = new Relationship();
            relationship.Patient = patient;
            relationship.SupportPerson = supportPerson;
            relationship.Type = RelationshipType.Aunt;     
       
            Container.Resolve<IRelationshipRepository>().Save(relationship);
            patient.Relationships.Add(relationship);
            
            //patient.Relationships.Add();
            
            patientRepository.Save(patient);
            patientRepository.Flush();
            patientRepository.Clear();
            patient = patientRepository.GetByID(patient.Id);
        }

        [Test]
        public void can_save_with_address()
        {                                  
            patient.Address.City.ShouldEqual("Hunlock Creek");
            patient.Address.State.ShouldEqual("PA");
            patient.Address.Street1.ShouldEqual("Box 448");
            patient.Address.PostalCode.ShouldEqual("48103");
            patient.County.ShouldEqual(county);
        }

        [Test]
        public void can_save_notes()
        {
            patient.Notes[0].Body.ShouldEqual(body);
            patient.Notes[0].StaffId.ShouldEqual("Foo");
            patient.Notes[0].Date.ShouldEqual(date);
        }

        [Test]
        public void can_save_relationships()
        {
            patient.Relationships.Count.ShouldEqual(1);
        }

        [Test]
        public void support_person_also_has_relationship()
        {
            var supportPersonRepository = Container.Resolve<SupportPersonRepository>();
            supportPersonRepository.Clear();
            supportPerson = supportPersonRepository.GetByID(supportPerson.Id);

            // can't know which relationship it is if this test has run more than once.
            foreach (var relationship in supportPerson.Relationships)
            {
                if (relationship.Patient.Equals(patient))
                    return;
            }
            
            throw new Exception("Test fails, relationship not found");
        }

        [Test]
        public void can_save_stays()
        {
            patient.Stays[0].ShouldEqual(stay);            
        }

        [Test]
        public void can_save_diagnoses()
        {
            patient.Diagnoses.Count.ShouldEqual(1);
        }
    }
}